package com.liushuyv.calculator.ui;
import java.util.Scanner;
import com.liushuyv.calculator.factory.AddFactory;
import com.liushuyv.calculator.factory.SubFactory;
import com.liushuyv.calculator.bll.IOperation;
import com.liushuyv.calculator.factory.IFactory;

/*
 * 运算器5：
 * 1.界面逻辑可以修改
 * 2.之前的工厂是一个大工厂，功能太多了，现在整出一个抽象工厂类Ifactory类后，细分出很多小工厂，实现功能单一化，降低耦合度
 * 3.缺陷：只能实现一种数据类型：int，想适应多种类型-》使用泛型

 */
class Funs<T extends Number>{
    public void commonCalculate(T numberA,T numberB,String oprType)throws Exception{
        IOperation<T> opr = null;
        IFactory<T> factory = null;
        T result;
        switch (oprType){
        case "+":
        factory = new AddFactory<T>();//指定工厂类的分工厂
        break;
        case "-":
        factory = new SubFactory<T>();
        break;//生成不同的模式
default:
        throw new Exception("运算符错了");
        }

        opr=factory.getOperation();//新增：生产运算类对象

        opr.setValue(numberA,numberB);//新增：给生成的对象赋值

        result = opr.getResult();//多态的体现
        System.out.println(result);
        }
        }

public class Test {
    public static void main(String[] args) throws Exception {
        /*整型加法*/
        Integer numberA, numberB;
        String oprType;
        Scanner scanner = new Scanner(System.in);
        numberA = scanner.nextInt();
        numberB = scanner.nextInt();
        oprType = scanner.next();
        Funs<Integer> funs = new Funs<>();
        funs.commonCalculate(numberA, numberB, oprType);
        Double numberDA, numberDB;
        numberDA = scanner.nextDouble();
        numberDB = scanner.nextDouble();
        new Funs<Double>().commonCalculate(numberDA, numberDB, oprType);
    }
}
